package com.magic.gre.base.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;


/**
 * Author: Lmoumou
 * Date: 2018-09-13 08:47
 * 文件名: BaseDialog
 * 描述:
 */
@SuppressLint("ValidFragment")
public abstract class BaseDialog extends DialogFragment {

    private DismissListanner dismissListanner;

    protected Context mContext;

    public void setDismissListanner(DismissListanner dismissListanner) {
        this.dismissListanner = dismissListanner;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context.getApplicationContext();
    }

    /**
     * 弹窗消失
     */
    protected void dismissThis(boolean isResume) {
        if (isResume) {
            dismiss();
        } else {
            dismissAllowingStateLoss();
        }
    }

    public void showThis(FragmentManager manager, String tag) {
        if (!this.isAdded()) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            if (setDialogWith() != 0F) {
                dialog.getWindow().setLayout((int) (dm.widthPixels * setDialogWith()), ViewGroup.LayoutParams.WRAP_CONTENT);
            } else {
                dialog.getWindow().setLayout((int) (dm.widthPixels * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
            }

            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            params.gravity = setDialogPosition();
            params.dimAmount = halfTpValue();
            dialog.getWindow().setAttributes(params);
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, com.noname.lib_base_java.R.style.BaseFragmentDialog);
        Bundle bundle = getArguments();
        if (bundle != null) {
            initBundle(bundle);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setWindowAnimations(setAnimId());
        if (!clickBack()) {
            getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    return i == KeyEvent.KEYCODE_BACK;
                }
            });
        }
        if (setViewId() != 0) {
            super.onCreateView(inflater, container, savedInstanceState);
            View view = inflater.inflate(setViewId(), container);
            return view;
        }
        return super.onCreateView(inflater, container, savedInstanceState);

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setCanceledOnTouchOutside(setOutSide());
        return dialog;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dismissListanner != null) {
            dismissListanner.onDialogMiss();
        }
    }

    public interface DismissListanner {
        void onDialogMiss();
    }

    /**
     * 点击外部是否消失
     */
    protected abstract boolean setOutSide();

    /**
     * 初始化View
     */
    protected abstract void initView(View view);

    /**
     * 布局id
     */
    protected abstract int setViewId();

    /**
     * 返回键是否屏蔽
     */
    protected abstract boolean clickBack();

    /**
     * 显示动画
     */
    protected abstract int setAnimId();

    /**
     * 传入值
     */
    protected abstract void initBundle(Bundle bundle);

    /**
     * 背景透明度
     *
     * @return
     */
    protected abstract float halfTpValue();

    /**
     * dialog的位置
     */
    protected abstract int setDialogPosition();

    /**
     * 宽所占屏幕的比例
     */
    protected abstract Float setDialogWith();
}
